﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Convertor.Interface;
using Wpf.CommonTools.SystemExtension;
using Wpf.CommonTools.StringExtension;

namespace Convertor.Plugin.Filter
{
    public enum ReturnTypes
    {
        OriginalText,
        FilterText,
        Replace,
        StringFormat
    }

    public enum ReplaceTypes
    {
        CustomText,
        InputContent,
        ToUpper,
        ToLower
    }

    [Serializable]
    public class ConfigContent : IConfigContent, ISerializable, ICloneable, INotifyPropertyChanged
    {

        /// <summary>
        /// 
        /// </summary>
        private string filterText;

        public string FilterText
        {
            get { return filterText ?? (filterText = ""); }
            set { filterText = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private string replaceText;

        /// <summary>
        /// 
        /// </summary>
        public string ReplaceText
        {
            get { return replaceText ?? (replaceText = ""); }
            set { replaceText = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private string memoryAddress;

        /// <summary>
        /// 
        /// </summary>
        public string MemoryAddress
        {
            get { return memoryAddress ?? (memoryAddress = ""); }
            set { memoryAddress = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private ReturnTypes returnType = ReturnTypes.FilterText;

        /// <summary>
        /// 
        /// </summary>
        public ReturnTypes ReturnType
        {
            get { return returnType; }
            set { returnType = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }
        private ReplaceTypes replaceType = ReplaceTypes.CustomText;
        /// <summary>
        /// 
        /// </summary>
        public ReplaceTypes ReplaceType
        {
            get { return replaceType; }
            set { replaceType = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }

        private RegexOptions regexOption = RegexOptions.IgnoreCase | RegexOptions.Singleline;
        /// <summary>
        /// 
        /// </summary>
        public RegexOptions RegexOption
        {
            get { return regexOption; }
            set { regexOption = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ToolTip")); } }
        }

        public ConfigContent()
        {
        }

        public override string ToString()
        {
            return ToolTip;
        }

        #region IToolTip 成员

        public string ToolTip
        {
            get
            {
                string value;
                switch (returnType)
                {
                    default:
                    case ReturnTypes.FilterText:
                        value = string.Format(UILanguage.TryFindResource("Plugin_Filter_Info_FilterText"), FilterText.ToLimitLength(100));
                        break;
                    case ReturnTypes.OriginalText:
                        value = string.Format(UILanguage.TryFindResource("Plugin_Filter_Info_OriginalText"), FilterText.ToLimitLength(100));
                        break;
                    case ReturnTypes.Replace:
                        value = string.Format(UILanguage.TryFindResource("Plugin_Filter_Info_Replace"), FilterText.ToLimitLength(100), ReplaceText.ToLimitLength(100));
                        break;
                }
                return value;
            }
        }

        #endregion

        #region ISerializable 成员

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("FilterText", FilterText, typeof(string));
            info.AddValue("ReplaceText", ReplaceText, typeof(string));
            info.AddValue("ReturnType", ReturnType, typeof(ReturnTypes));
            info.AddValue("RegexOption", RegexOption, typeof(RegexOptions));
            info.AddValue("ReplaceType", ReplaceType, typeof(ReplaceTypes));
        }

        public ConfigContent(SerializationInfo info, StreamingContext context)
        {
            // Reset the property value using the GetValue method.
            filterText = (string)info.GetValue("FilterText", typeof(string));
            replaceText = (string)info.GetValue("ReplaceText", typeof(string));
            returnType = (ReturnTypes)info.GetValue("ReturnType", typeof(ReturnTypes));
            regexOption = (RegexOptions)info.GetValue("RegexOption", typeof(RegexOptions));
            try
            {
                replaceType = (ReplaceTypes)info.GetValue("ReplaceType", typeof(ReplaceTypes));
            }
            catch { }
        }

        #endregion

        #region ICloneable 成员

        public object Clone()
        {
            ConfigContent content = new ConfigContent()
            {
                FilterText = this.FilterText,
                RegexOption = this.RegexOption,
                ReplaceText = this.ReplaceText,
                ReturnType = this.ReturnType,
                ReplaceType = this.ReplaceType,
            };
            return content;
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

    }
}
